<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {	
	 function __construct()
	 {
	  	parent::__construct();
	  	$this->load->model('TB_User');
	  	$this->load->library('session');
	 }
	 function index()
	{
		$this->RefreshUser();
	//	$data["results"]=$this->TB_User->GetAllUser();
	//	$this->load->view("view_user",$data);
	}
	 
	function Insert()
	{
		$UserEnt=array("UserName"=>$_POST["UserName"],"Password"=>$_POST["Password"]);
		$this->TB_User->insert($UserEnt); 
		$this->RefreshUser();
	}
	function Update()
	{
		$UserEnt=array("UserID"=>$_POST["UserID"], "UserName"=>$_POST["UserName"],"Password"=>$_POST["Password"]);
		$this->TB_User->Update($UserEnt); 
		$this->RefreshUser();
	}
	public function delete()
	{
		$this->TB_User->delete($_GET["UserID"]); 
		$this->RefreshUser();
	}
	function GetAllUser()
	{
		return $TB_User->GetAllUser();
	}
	function GetUserByUserID($UserID)
	{
		return 	$this->TB_User->GetUserByID($UserID);
	}
	function UserIsExist($UserName,$Password)
	{
		return 	$this->TB_User->UserIsExist($UserName,$Password);
	}
	function UserLogin()
	{
		if($this->UserIsExist($_POST['UserName'],$_POST['Password']))
		{
		//	$newdata = array(
         //          'UserName'  => $_POST['UserName'],
         //          'logged_in' => TRUE
         //      );
		//	$this->session->set_userdata($newdata);
		//	$this->load->view("manage/index");
		$this->session->set_userdata('UserName',$_POST['UserName']);
			$this->session->set_userdata('logged_in',TRUE);
			redirect(base_url().'main', 'refresh');
		}
		else
		{
		//	$newdata = array(
          //        'UserName'  => $_POST['UserName'],
           //        'logged_in' => false
            //   );
		//	$this->session->set_userdata($newdata);
		$this->session->set_userdata('UserName',$_POST['UserName']);
				$this->session->set_userdata('logged_in',false);
		//	$this->load->view('login/errindex');
			redirect(base_url().'login/errindex', 'refresh');
		}
	}
	function RefreshUser()
	{
		$data["results"]=$this->TB_User->GetAllUser();
		$this->load->view("view_user",$data);
	}
}